import os
import sys


def main():
    HASH_TYPE = "md5"

##    if HASH_TYPE == "md5":
##    #                print "md5"
##                    m = hashlib.md5()
##    else:
##     #               print "sha1"
##                    m = hashlib.sha1()

    ##if(HASH_TYPE == "sha1"):
    ##        m.update('The quick brown fox jumps over the lazy dog')
    ##        a = '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'
    ##        b = m.hexdigest()
    ##        print a == b



   

    checksum = raw_input("enter the " + HASH_TYPE + " value: ")
    #print "choose the file for " + HASH_TYPE + "checksum: "

    
    count = 0
    while(True):
        testFile = fileChoose('rb')
        if(testFile != None):
	    if(len(checksum) > 0):
                hashVal = fileHashCheck(testFile,HASH_TYPE)
                if(hashVal == checksum):
                    print 'True' # will produce what you're looking for
                else:
                    print hashval
                    print 'False'
	    else:
	        print  fileHashCheck(testFile,HASH_TYPE)
            break
        elif(count<1):
            print "No file Choosen, Choose a file: "
            count = count+1
        else:
            break
    raw_input("Press enter to exit...") 

    

def fileChoose(readMode):
    import Tkinter
    import tkFileDialog
    t = Tkinter.Tk()
    #t.focus()
    t.withdraw()  #hides the tk root window
    path = os.path.expanduser('~/Desktop')
    fileTypes = [ ('archives' , ' .zip .tar .gz .iso') , ('text files', '.txt') , ('all files', '.*')]
    testFile =  tkFileDialog.askopenfile(initialdir=path , parent=None, filetypes= fileTypes, mode=readMode,title='Choose a file to calculate checksum')
    return testFile



def fileHashCheck(testFile, HASH_TYPE):
    import hashlib
    print "fileHashCheck"
    if(HASH_TYPE == "md5"):
                hash = hashlib.md5()
    else:
                hash = hashlib.sha1()

    while True:
        piece = testFile.read(1024)

        if piece:
                hash.update(piece)
        else: # we're at end of file
            #change by shibli
            hash.update(piece)
            hex_hash = hash.hexdigest()
            break
    return hex_hash.lower()





if __name__ == '__main__':
    main()




